home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / ntfs / user / ntgrep.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-15  |  2.9 KB  |  126 lines

  1. /*
  2.  *  ntgrep.c
  3.  *
  4.  *  Copyright (C) 1995-1997 Martin von L÷wis
  5.  *  Copyright (C) 1997 RΘgis Duchesne
  6.  */
  7.  
  8. #include "ntfstypes.h"
  9. #include "struct.h"
  10. #include "nttools.h"
  11. #include "dump.h"
  12. #include "config.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #ifdef HAVE_GETOPT_H
  16. #include <getopt.h>
  17. #else
  18. #define getopt_long(a,v,o,ol,x)        getopt(a,v,o)
  19. #endif
  20. #ifdef HAVE_UNISTD_H
  21. #include <unistd.h>
  22. #endif
  23.  
  24. #define GREP_DISPLAY_SIZE 512
  25.  
  26. char *short_opts="af:o:inCB:bc:";
  27. #ifdef HAVE_GETOPT_H
  28. struct option options[]={
  29.     {"filesystem",1,0,'f'},
  30.     {"offset",1,0,'o'},
  31.     {"ignorecase",0,0,'i'},
  32.     {"ascii",0,0,'a'},
  33.     {"nodump",0,0,'n'},
  34.     {"continue",0,0,'C'},
  35.     {"cluster",1,0,'c'},
  36.     {"blocksize",1,0,'B'},
  37.     {"bytes",0,0,'b'},
  38.     {0,0,0,0}
  39. };
  40. #endif
  41.  
  42. void usage(void)
  43. {
  44.     fprintf(stderr,"ntgrep <options> string\n"
  45.         "  --filesystem, -f device  use device as volume\n"
  46.         "  --offset, -o n           start at offset n\n"
  47.         "  --ignorecase, -i         do caseless search\n"
  48.         "  --ascii, -a              search for ASCII string (default is Unicode)\n"
  49.         "  --nodump, -n             display only location, don't dump context\n"
  50.         "  --continue, -C           continue searching until end of volume\n"
  51.         "  --cluster, -c n          start at cluster n\n"
  52.         "  --blocksize, -b n        dump n bytes around the location\n"
  53.         "  --bytes                  string is given as hex bytes\n"
  54.         );
  55. }
  56.  
  57. int main(int argc,char *argv[])
  58. {
  59.     int c;
  60.     int ignore_case=0,ascii=0,cont=0,bytes=0;
  61.     char *device=0;
  62.     ntfs_size_t offset=0;
  63.     int blocksize=GREP_DISPLAY_SIZE;
  64.     ntfs_size_t pos,length;
  65.     char match[2048];
  66.     char *in;
  67.     extern int opterr,optind;
  68.     extern char* optarg;
  69.     ntfs_volume *volume;
  70.  
  71.     opterr=1;
  72.     while((c=getopt_long(argc,argv,short_opts,options,NULL))>0)
  73.         switch(c)
  74.         {
  75.         case 'f': device=optarg;break;
  76.         case 'o': offset=strtol(optarg,NULL,0);break;
  77.         case 'i': ignore_case=1;break;
  78.         case 'a': ascii=1;break;
  79.         case 'C': cont=1;break;
  80.         case 'c': offset=strtol(optarg,NULL,0)*512/*FIXME*/;break;
  81.         case 'B': blocksize=strtol(optarg,NULL,0);break;
  82.         case 'b': bytes=1;break;
  83.         }
  84.     if(optind==argc){
  85.         usage();
  86.         return 1;
  87.     }
  88.     in=argv[optind];
  89.     if(bytes)
  90.     {       char buf[3];
  91.     buf[2]='\0';
  92.     for(length=0;*in && *(in+1);in+=2,length++)
  93.     {       buf[0]=in[0];
  94.     buf[1]=in[1];
  95.     match[length]=strtol(buf,NULL,16);
  96.     }
  97.     }
  98.     else if(ascii)
  99.     {       strcpy(match,in);
  100.     length=strlen(in);
  101.     }else
  102.         for(length=0;*in;in++,length+=2)
  103.         {       match[length]=*in;
  104.         match[length+1]='\0';
  105.         }
  106.     volume=ntfs_open_volume(device,0,0,1);
  107.     do{
  108.         pos=grep(volume,offset,-1,match,length,ignore_case);
  109.         if(pos==-1)
  110.             fprintf(stderr,"Not found\n");
  111.         else{
  112.             printf("0x%X\n",pos);
  113.             pos=(pos/blocksize)*blocksize;
  114.             dump(volume,pos,pos,blocksize);
  115.             offset=pos+blocksize;
  116.         }
  117.     }while(cont);
  118.     return 0;
  119. }
  120.  
  121. /*
  122.  * Local variables:
  123.  * c-file-style: "linux"
  124.  * End:
  125.  */
  126.